package com.story.pr;

import java.util.ArrayList;
import com.story.pr.function.StoryDetails;
import com.story.pr.function.StoryRead;
import com.story.pr.function.StorySearch;
import android.app.AlertDialog;
import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Toast;

public class StoryProject extends ListActivity implements OnClickListener {

	protected ArrayList<StoryList> array = null;
	ProgressDialog strDialog = null;
	protected StoryAdapter sadapter;
	Runnable runStrfinal;
	final String URL = "http://10.0.2.2:57232/Service1.asmx";
	private static final int SEARCH_COMICS = Menu.FIRST;
	private static final int ABOUT = Menu.FIRST + 2;
	ImageView imvSearch;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		ListView list1 = getListView();
		array = new ArrayList<StoryList>();
		this.sadapter = new StoryAdapter(this, R.layout.list, array);
		list1.setAdapter(sadapter);

		runStrfinal = new Runnable() {

			public void run() {
				getStory();
			}

		};

		Thread threadStr = new Thread(runStrfinal);
		threadStr.start();

		strDialog = ProgressDialog.show(this, "Xin hãy đợi...",
				"Đang tải dữ liệu", false);

		imvSearch = (ImageView) findViewById(R.id.search);
		imvSearch.setOnClickListener(this);

	}

	// Hien thi thong bao ket qua tim kiem
	public void createDialog(String title, String text) {
		AlertDialog ad = new AlertDialog.Builder(this)
				.setPositiveButton("Ok", null).setTitle(title).setMessage(text)
				.create();
		ad.show();
	}

	// Tạo Option Menu
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu(menu);
		menu.add(0, SEARCH_COMICS, 0, "Search").setIcon(
				android.R.drawable.ic_search_category_default);
		menu.add(0, ABOUT, 0, "About").setIcon(
				android.R.drawable.ic_menu_info_details);
		return true;
	}

	// ////////////////////////////////////////////////////////////////

	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case SEARCH_COMICS: {
			searchStory();
			break;
		}
		case ABOUT: {
			AlertDialog.Builder ab = new AlertDialog.Builder(this);
			ab.setTitle("Nhóm phát triển :");
			ab.setMessage("1. Đặng Đình Hợp" + "\n" + "2. Nguyễn Văn Tâm"
					+ "\n" + "3. Ngô Thanh Tùng ");
			ab.setPositiveButton("Close",
					new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialog, int which) {
						}
					});
			ab.setIcon(android.R.drawable.ic_dialog_info);
			ab.show();
			break;
		}
		}
		return true;
	}

	// /////////////////////////////////////////////
	// Phương thức chuyển sang trang Search
	// /////////////////////////////////////////////
	public void searchStory() {
		Intent i = new Intent();
		i.setAction("Search");
		i.setClass(this, StorySearch.class);
		startActivityForResult(i, 0);
	}

	// ////////////////////////////////////////////
	// Sự kiện click Item Listview
	// ///////////////////////////////////////////
	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		// TODO Auto-generated method stub
		super.onListItemClick(l, v, position, id);

		StoryList sl = (StoryList) l.getItemAtPosition(position);

		try {
			Bundle sendBundle = new Bundle();
			sendBundle.putString("short_title", sl.short_title);
			sendBundle.putString("cover", sl.cover);
			sendBundle.putString("title", sl.title);
			sendBundle.putString("description", sl.description);
			sendBundle.putInt("rate", sl.rate);

			// //Tạo Intent để khởi chạy Activity2 và gắn sendBundble vào Intent
			Intent i = new Intent(StoryProject.this, StoryDetails.class);
			i.putExtras(sendBundle);
			startActivity(i);
			// //Giải phóng Activity1 khỏi Activity Stack vì ta sẽ ko quay lại
			// nó nữa
			// finish();
		} catch (Exception e) {

			Toast.makeText(getApplicationContext(), e.toString(),
					Toast.LENGTH_SHORT).show();
		}
	}

	// //////////////////////////////////////////////////
	// Phuong thuc gan du lieu vao Arraylist
	// /////////////////////////////////////////////////
	public ArrayList<StoryList> getStoryLists(Story newStr) {
		ArrayList<StoryList> str1 = new ArrayList<StoryList>();
		for (int i = 0; i < newStr.slbg; i++) {
			StoryList str2 = new StoryList();
			str2.cover = newStr.cover.get(i).toString();
			str2.short_title = newStr.short_title.get(i).toString();
			str2.title = newStr.title.get(i).toString();
			str2.description = newStr.description.get(i).toString();
			str2.rate = Integer.valueOf(newStr.rate.get(i).toString());
			str1.add(str2);
		}
		return str1;
	}

	// //////////////////////////////////////
	// Phuong thuc lay truyen
	// /////////////////////////////////////
	public void getStory() {
		try {

			Story newStr = new Story();

			newStr = newStr.getStoryAll("str");
			array = getStoryLists(newStr);
			Thread.sleep(500);

		} catch (Exception e) {
			System.out.println("Loi" + e);
		}

		runOnUiThread(runStr);
	}

	Runnable runStr = new Runnable() {

		public void run() {

			if (array != null && array.size() > 0) {
				sadapter.notifyDataSetChanged();
				for (int i = 0; i < array.size(); i++)
					sadapter.add(array.get(i));
			}
			strDialog.dismiss();
			sadapter.notifyDataSetChanged();
		}
	};

	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.search:
			searchStory();
			break;
		}
	}

}